/**
 *
 */
package com.wedroid.r2d2.support;

import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.time.ZonedDateTime;
import java.util.Map;

import org.apache.commons.codec.digest.DigestUtils;

import com.alibaba.fastjson.annotation.JSONField;

/**
 * @author lute
 */
public final class MediaRequest {

    @JSONField(name = "UploadType")
    private int uploadType;

    @JSONField(name = "BaseRequest")
    private Map<String, Object> baseRequest;

    @JSONField(name = "ClientMediaId")
    private long clientMeidaId;

    @JSONField(name = "TotalLen")
    private long totalLen;

    @JSONField(name = "StartPos")
    private int startPos;

    @JSONField(name = "DataLen")
    private long dataLen;

    @JSONField(name = "MediaType")
    private int mediaType;

    @JSONField(name = "FromUserName")
    private String fromUserName;

    @JSONField(name = "ToUserName")
    private String toUserName;

    @JSONField(name = "FileMd5")
    private String fileMd5;

    /**
     * @param baseRequest
     * @param path
     * @param fromUserName
     * @param toUserName
     */
    public MediaRequest(BaseRequest baseRequest, Path path, String fromUserName, String toUserName) {
        this.uploadType = 2;
        this.baseRequest = baseRequest.get();
        this.clientMeidaId = ZonedDateTime.now().toEpochSecond();
        this.totalLen = path.toFile().length();
        this.startPos = 0;
        this.dataLen = this.totalLen;
        this.mediaType = 4;
        this.fromUserName = fromUserName;
        this.toUserName = toUserName;

        try (InputStream inputStream = Files.newInputStream(path)) {
            this.fileMd5 = DigestUtils.md5Hex(inputStream);
        } catch (IOException e) {
            throw new IllegalStateException(e);
        }
    }

    /**
     * @return the uploadType
     */
    public int getUploadType() {
        return uploadType;
    }

    /**
     * @return the baseRequest
     */
    public Map<String, Object> getBaseRequest() {
        return baseRequest;
    }

    /**
     * @return the clientMeidaId
     */
    public long getClientMeidaId() {
        return clientMeidaId;
    }

    /**
     * @return the totalLen
     */
    public long getTotalLen() {
        return totalLen;
    }

    /**
     * @return the startPos
     */
    public int getStartPos() {
        return startPos;
    }

    /**
     * @return the dataLen
     */
    public long getDataLen() {
        return dataLen;
    }

    /**
     * @return the mediaType
     */
    public int getMediaType() {
        return mediaType;
    }

    /**
     * @return the fromUserName
     */
    public String getFromUserName() {
        return fromUserName;
    }

    /**
     * @return the toUserName
     */
    public String getToUserName() {
        return toUserName;
    }

    /**
     * @return the fileMd5
     */
    public String getFileMd5() {
        return fileMd5;
    }

}
